#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 510;
int f[N][N], arr[N];
int s[N];
int n;
int dfs(int l, int r)
{
	if (f[l][r] != -1)return f[l][r];
	if (l == r) {
		f[l][r] = 0;
		return f[l][r];
	}
	int ans = 1 << 30;
	for (int i = l; i < r; i++) {
		ans = min(ans, dfs(l, i) + dfs(i + 1, r) + s[r] - s[l - 1]);
	}
	f[l][r] = ans;
	return f[l][r];
}
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)cin >> arr[i], s[i] = s[i - 1] + arr[i];
	memset(f, -1, sizeof(f));
	cout << dfs(1, n) << endl;
	return 0;
}